// pages/mine/address/detail/detail.js
var app = getApp();
var url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showss: false,
    shows: false,
    show: false,//控制下拉列表的显示隐藏，false隐藏、true显示
    selectData: ['1', '2', '3', '4', '5', '6'],//下拉列表的数据
    selectDatas: ['1', '2', '3', '4', '5', '6'],//下拉列表的数据
    selectDatass: ['1', '2', '3', '4', '5', '6'],//下拉列表的数据
    index: 0,//选择的下拉列表下标
    provinceid: '',
    cityid: '',
    areaid: '',
    cityname: '',
    provincename: '',
    areaname: '',
    index:'',
    name:'',
    phone:'',
    map:'',
    addressid:'',
  },
  name: function (e) {
    console.log(e.detail.value);
    this.setData({
      name: e.detail.value
    });
    console.log(this.data.name);
  },
  phone: function (e) {
    console.log(e.detail.value);
    this.setData({
      phone: e.detail.value
    })
  },
 map: function (e) {
    this.setData({
      map: e.detail.value
    });
  },
  //修改地址
  address:function(e){
    var pages = getCurrentPages();
    var beforePage = pages[pages.length - 2]; // 前一个页面  
    console.log(beforePage);

    var addressid = e.currentTarget.dataset.id;
    console.log(addressid);
    var name = this.data.name;
    console.log(name);
    var phone = this.data.phone;
    var map = this.data.map;
    console.log(map);
    var province = this.data.provinceid;
    var city = this.data.cityid;
    var district = this.data.areaid;
    console.log(province);
    console.log(city);
    console.log(district);
    var that = this;
    wx.request({
      url: url+'editUserAddress',
      data: {
        addressId: addressid,
        consignee: name,
        country: 1,
        province: province,
        city: city,
        district: district,
        address: map,
        mobile: phone,
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        console.log(res);
        beforePage.onLoad();//执行上一个页面的方法
        if(res.data.resultCode == "000000"){
          wx.showToast({
            title: '保存成功',
            icon: '',
            image: '',
            duration: 1000,
            mask: true,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
          })
        }else{
          wx.showToast({
            title: '保存失败,请重试',
            icon: '',
            image: '',
            duration: 2000,
            mask: true,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
          })
        }
      }
    })
  },
  //删除地址
  del:function(e){
    wx.showModal({
      title: '确定删除？',
      success: function (res) {
        if (res.confirm) {
          var addressid = e.currentTarget.dataset.id;
          var that = this;
          wx.request({
            url:url+ 'removeMemeberUserAddressById',
            data: {
              id: addressid,
            },
            header: {
              "Content-Type": "application/json"
            },
            success: function (res) {
              console.log(res.data.object)
              if (res.data.object == true) {
                wx.showToast({
                  title: '删除成功！',
                  duration: 1000,
                  mask: true,
                  success: function (res) {
                    wx: wx.navigateTo({
                      url: '../address',
                    }) 
                  },
                })
              } else {
                wx.showToast({
                  title: '删除失败！',
                  duration: 2000,
                  mask: true,
                  success: function (res) { },
                  fail: function (res) { },
                  complete: function (res) { },
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //设置默认地址
  isshow: function (e) {
    var pages = getCurrentPages();
    var beforePage = pages[pages.length - 2]; // 前一个页面  
    console.log(beforePage);

    console.log(e);
    var isaddress = e.currentTarget.dataset.address;
    var addressid = e.currentTarget.dataset.id;
    const userid = app.globalData.userid;
    var that = this;
    if(isaddress == 0){
      wx.request({
        url: url+'editUserAddressDefault',
        data: {
          userId: userid,
          addressId: addressid,
          state: 1,
        },
        header: {
          "Content-Type": "application/json"
        },
        success: function (res) {
          // console.log(res.data.object)
          if (res.data.object == true) {
            wx.showToast({
              title: '设置成功',
              icon: '',
              image: '',
              duration: 2000,
              mask: true,
              success: function (res) {
                beforePage.onLoad();//执行上一个页面的方法
              },
            })
          }
        }
      })
    }
    if(isaddress == 1){
      wx.request({
        url: url+'editUserAddressDefault',
        data: {
          userId: userid,
          addressId: addressid,
          state: 0,
        },
        header: {
          "Content-Type": "application/json"
        },
        success: function (res) {
          // console.log(res.data.object)
          if (res.data.object == true) {
            wx.showToast({
              title: '取消成功',
              icon: '',
              image: '',
              duration: 2000,
              mask: true,
              success: function (res) {
                beforePage.onLoad();//执行上一个页面的方法
              },
            })
            
          }
        }
      })
    }
    this.detail();
  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  
  //初始页面详情
  detail:function(){
    var index = this.data.index;
    const userid = app.globalData.userid;
    var that = this;
    wx.request({
      url: url+ 'getMemberAddressByUserId',
      data: {
        userId: userid,
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        //  console.log(res.data.object)
        that.setData({
          address: res.data.object[index],
          phone: res.data.object[index].mobile,
          map: res.data.object[index].address,
          name: res.data.object[index].consignee,
          provincename: res.data.object[index].provinceName,
          cityname: res.data.object[index].cityName,
          areaname: res.data.object[index].areaName,
        })
      }
    })
  },
  onLoad: function (options) {
     var index = options.index;
    //  console.log(index);
     this.data.index = index;
     console.log(this.data.index);
     var that = this;
     that.setData({
       index:index
     })
     this.detail();
    
  },
  
  // 点击下拉显示框
  selectTap() {
    this.setData({
      show: !this.data.show
    });
    var that = this;
    //省级
    wx.request({
      url: url+'getAllProvince',
      data: {
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        console.log(res.data.object)
        that.setData({
          provinces: res.data.object
        })
      }
    })
  },
  // 点击下拉列表
  optionTap(e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    // var provinceid = id;
    let name = e.currentTarget.dataset.name;//获取点击的下拉列表的下标
    this.setData({
      id: id,
      index: index,
      provincename: name,
      show: !this.data.show,
      provinceid:id,
    });
  },

  //市级
  // 点击下拉显示框
  selectTaps() {
    this.setData({
      shows: !this.data.shows
    });
    var that = this;
    var provinceid = this.data.provinceid;
    //市级
    wx.request({
      url: url+'getCityByProvinceId',
      data: {
        provinceId: provinceid
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        console.log(res.data.object)
        that.setData({
          citys: res.data.object
        })
      }
    })
  },
  // 市级点击下拉列表
  optionTaps(e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    // app.globalData.cityid = id;
    let name = e.currentTarget.dataset.name;//获取点击的下拉列表的下标
    this.setData({
      id: id,
      index: index,
      cityname: name,
      shows: !this.data.shows,
      cityid:id,
    });
  },

  //区级
  // 点击下拉显示框
  selectTapss() {
    this.setData({
      showss: !this.data.showss
    });
    var that = this;
    var cityid = this.data.cityid;
    //区级
    wx.request({
      url: url+'getAreaByCityId',
      data: {
        cityId: cityid
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        console.log(res.data.object)
        that.setData({
          areas: res.data.object
        })
      }
    })
  },
  // 市级点击下拉列表
  optionTapss(e) {
    var id = e.currentTarget.dataset.id;
    // app.globalData.areaid = id;
    var index = e.currentTarget.dataset.index;
    let name = e.currentTarget.dataset.name;//获取点击的下拉列表的下标
    this.setData({
      id: id,
      index: index,
      areaname: name,
      showss: !this.data.showss,
      areaid:id,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})